import 'package:flutter/material.dart';

void main() => runApp(MyApp());

//  生命周期
// 1.初始化数据
// 2.发送网络请求
// 3.监听事件
// 4.管理内存

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Material App',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Material App Bar'),
        ),
        body: HomeContent(),
      ),
    );
  }
}

// class HomeContent extends StatelessWidget {
//   final String message;
//   HomeContent(this.message){
//     print("构造方法被调用");
//   }
//   @override
//   Widget build(BuildContext context) {
//     print("build方法被调用");
//     return
//     Text(message);
//   }
// }

/**
 * statefulwidget的生命周期
 * 
 */
class HomeContent extends StatefulWidget {
  HomeContent() {
    print("1.调用HomeContent的constructor");
  }
  @override
  _HomeContentState createState() {
    print("2.调用HomeContent的createState");
    return _HomeContentState();
  }
}

class _HomeContentState extends State<HomeContent> {
  int _counter = 0;
  _HomeContentState() {
    print("3.调用_HomeContentState的constructor");
  }
  @override
  void initState() {
    // 强调：这个地方是必须调用super的
    super.initState();
    print("4.调用_HomeContentState的initState");
  }

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
    print("调用_HomeContentState的didChangeDependencies");
  }

  @override
  void didUpdateWidget(HomeContent oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
    // 父类的widget发生改变时调用
    print("调用_HomeContentState的didUpdateWidget");
  }
//  @override
//  bool get mounted {
//    print("mounted");
//    return true;
//  }

  @override
  Widget build(BuildContext context) {
    print("5.调用_HomeContentState的build");
    return Column(
      children: <Widget>[
        RaisedButton(
          child: Icon(Icons.add),
          onPressed: () {
            setState(() {
              _counter++;
            });
          },
        ),
        Text("数字：$_counter"),
      ],
    );
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    print("6.调用_HomeContentState的dispose");
  }
}
